home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / ANIM / ANTICIPA.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  5.5 KB  |  168 lines

  1.  
  2. package sub_arctic.anim;
  3. import java.awt.Point;
  4.  
  5. /**
  6.  * This class implements a trajectory along a line, but with cartoonish
  7.  * anticipation and overshoot. The first 20% of the line (0.0-0.2) are
  8.  * spent in anticipation. We compute 10% of the distance traveled 
  9.  * (the dimensions x and y are dealt with separately) and move opposite
  10.  * the target direction that amount and then back to our starting point.
  11.  * This is done via a sin curve, so we are sure we end up where we started.
  12.  * As we go across this line we are adding 5% of the total distance 
  13.  * traveled into the value and modulating that 5% with a sin function
  14.  * so the object travels in a slightly curved path. The last 20% is 
  15.  * the overshoot, and we do that again with a sin.<p>
  16.  * 
  17.  * @author Ian Smith
  18.  */
  19. public class anticipation_line extends line_trajectory {
  20.  
  21.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  22.  
  23.   /**
  24.    * This is the amount of backup on anticipation. Defaults to 10%.
  25.    */
  26.   public static double antic_amount=0.1; /* 10% backup on anticipate  */
  27.  
  28.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  29.  
  30.   /**
  31.    * This is amount of curvature to put in the path.  Defaults to 5%.
  32.    */
  33.   public static double curve_amount=0.05; /* 5% extra curvature */
  34.  
  35.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  36.  
  37.   /**
  38.    * This is the amount of overshoot at the end.  Defaults to 10%.
  39.    */
  40.   public static double overshoot_amount=0.1; /* 10% overshoot */
  41.  
  42.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  43.  
  44.   /** 
  45.    * Where we start the anticipation and overshoot (in terms of time).
  46.    * Defaults to 0.2.
  47.    */
  48.   public static double cutoff_point=0.2;
  49.  
  50.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  51.  
  52.   /**
  53.    * Construct a cartoon line between two points.<P>
  54.    * 
  55.    * You should make sure that if you use a pacing function which is
  56.    * a slow_in_slow_out that the result of the slow part is 0.2 
  57.    * (== cutoff_point). <p>
  58.    *
  59.    * @param int x1 starting x coordinate
  60.    * @param int y1 starting y coordinate
  61.    * @param int x2 ending x coordinate
  62.    * @param int y2 ending y coordinate
  63.    */
  64.   public anticipation_line(int x1, int y1, int x2, int y2, pacer p) {
  65.     super(x1,y1,x2,y2,p);
  66.   }
  67.  
  68.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  69.  
  70.   /** 
  71.    * This does the work for this trajectory. It maps the current time
  72.    * onto a Point.<p>
  73.    *
  74.    * @param double parm the amount of time to be mapped
  75.    */
  76.   public Object object_for_parm(double parm) {
  77.     double parameter=_pacer.pace(parm);
  78.     double amountOfSine,sineval,dist_amount;
  79.     int intsignx,intsigny;
  80.     int newx, newy;
  81.     int our_delta_x, our_delta_y;
  82.  
  83.     /* set the sign variables up */
  84.     intsignx = _delta_x < 0 ? -1 : 1;
  85.     intsigny = _delta_y < 0 ? -1 : 1;
  86.  
  87.     /* we want to make sure that we get some curvature even on 
  88.        lines which have same starting and end points equal in some dimension */
  89.     if (Math.abs(_delta_x)<100) {
  90.       our_delta_x=100*intsignx;
  91.     } else {
  92.       our_delta_x=_delta_x;
  93.     } 
  94.  
  95.     if (Math.abs(_delta_y)<100) {
  96.       our_delta_y=100*intsigny;
  97.     } else {
  98.       our_delta_y=_delta_y;
  99.     }
  100.  
  101.     /* are we in the first .2? */
  102.     if (parameter<cutoff_point) {
  103.  
  104.       /* how much sine?*/
  105.       amountOfSine=parameter/0.2;
  106.       sineval=Math.sin(amountOfSine*Math.PI);
  107.  
  108.       /* compute new points */
  109.       newx=_x_origin-(int)(sineval * (((double)our_delta_x)*(antic_amount)));
  110.       newy=_y_origin-(int)(sineval * (((double)our_delta_x)*(antic_amount)));
  111.       return new Point(newx, newy);
  112.     }
  113.  
  114.     if (parameter<(1-cutoff_point)) {
  115.  
  116.       parameter-=0.2;
  117.       dist_amount=parameter/0.6;
  118.  
  119.       /* how much sine?*/
  120.       amountOfSine=parameter/0.6;
  121.       sineval=Math.sin(amountOfSine*Math.PI);
  122.  
  123.       /* compute new points */
  124.       newx=_x_origin+(int)(sineval * (((double)our_delta_x)*curve_amount));
  125.       newy=_y_origin+(int)(sineval * (((double)our_delta_y)*curve_amount));
  126.  
  127.       /* at this point we have only added the curvature, add the distance
  128.      traveled part now */
  129.       /* these need to be the real delta_x and y because this is the
  130.        * actual traversal part */
  131.       newx+=(int)(dist_amount * (double)_delta_x);
  132.       newy+=(int)(dist_amount * (double)_delta_y);
  133.       return new Point(newx,newy);
  134.     }
  135.  
  136.     /* final case: overshoot */
  137.     parameter-=0.8;
  138.     amountOfSine=parameter/0.2;
  139.     sineval=Math.sin(amountOfSine*Math.PI);
  140.  
  141.     /* compute new points */
  142.     newx=_x_origin+_delta_x+ /* this is real: amount traveled */
  143.       (int)(sineval * (((double)our_delta_x)*(overshoot_amount)));
  144.     newy=_y_origin+_delta_y+/* this is real: amount traveled */
  145.       (int)(sineval * (((double)our_delta_y)*(overshoot_amount)));
  146.  
  147.     return new Point(newx, newy);
  148.   }
  149.  
  150.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  151. }
  152. /*=========================== COPYRIGHT NOTICE ===========================
  153.  
  154. This file is part of the subArctic user interface toolkit.
  155.  
  156. Copyright (c) 1996 Scott Hudson and Ian Smith
  157. All rights reserved.
  158.  
  159. The subArctic system is freely available for most uses under the terms
  160. and conditions described in 
  161.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  162. and appearing in full in the lib/interactor.java source file.
  163.  
  164. The current release and additional information about this software can be 
  165. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  166.  
  167. ========================================================================*/
  168.